//
//  SalonSelectorCollectionViewCell.swift
//

import UIKit

class SalonSelectorCollectionViewCell: UICollectionViewCell {

    enum State {
        case collapsed
        case expanded

        var interLabelPadding: CGFloat {
            switch self {
            case .collapsed:
                return 6
            case .expanded:
                return 56
            }
        }

        var separatorWidth: CGFloat {
            switch self {
            case .collapsed:
                return 0
            case .expanded:
                return 240
            }
        }

        var separatorHeight: CGFloat {
            switch self {
            case .collapsed:
                return 0
            case .expanded:
                return 2
            }
        }

        var salonNameLeadingConstant: CGFloat {
            switch self {
            case .collapsed:
                return 20
            case .expanded:
                return 40
            }
        }

        var salonAddressLeadingConstant: CGFloat {
            switch self {
            case .collapsed:
                return 60
            case .expanded:
                return 80
            }
        }

        var backgroundColor: UIColor {
            switch self {
            case .collapsed:
                return UIColor.lightGray
            case .expanded:
                return .white
            }
        }

        var containerWidth: CGFloat {
            switch self {
            case .collapsed:
                return 250
            case .expanded:
                return 320
            }
        }

        var containerHeight: CGFloat {
            switch self {
            case .collapsed:
                return 150
            case .expanded:
                return 200
            }
        }
    }

    @IBOutlet weak var containerView: UIView!
    @IBOutlet weak var salonNameLabel: UILabel!
    @IBOutlet weak var salonAddressLabel: UILabel!
    @IBOutlet weak var separatorLine: UIView!
    
    @IBOutlet weak var interLabelsPaddingConstraint: NSLayoutConstraint!
    @IBOutlet weak var separatorLineWidthConstraint: NSLayoutConstraint!
    @IBOutlet weak var separatorLineHeightConstraint: NSLayoutConstraint!
    @IBOutlet weak var containerViewHeightConstraint: NSLayoutConstraint!
    @IBOutlet weak var containerViewWidthConstraint: NSLayoutConstraint!
    @IBOutlet weak var salonNameLeadingConstraint: NSLayoutConstraint!
    @IBOutlet weak var salonAddressLeadingConstraint: NSLayoutConstraint!

    var state: State = .collapsed {
        didSet {
            guard oldValue != state else { return }
            updateViewConstraints()
        }
    }

    private func updateViewConstraints() {
        containerView.backgroundColor = state.backgroundColor
        containerViewWidthConstraint.constant = state.containerWidth
        containerViewHeightConstraint.constant = state.containerHeight
        salonNameLeadingConstraint.constant = state.salonNameLeadingConstant
        salonAddressLeadingConstraint.constant = state.salonAddressLeadingConstant
        interLabelsPaddingConstraint.constant = state.interLabelPadding
        separatorLineWidthConstraint.constant = state.separatorWidth
        separatorLineHeightConstraint.constant = state.separatorHeight
        layoutIfNeeded()
    }

    func configure(with salon: SalonEntity) {
        salonNameLabel.text = salon.name
        salonAddressLabel.text = salon.address
        updateViewConstraints()
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        salonNameLabel.text = nil
        salonAddressLabel.text = nil
        state = .collapsed
    }
}
extension UICollectionViewCell {
    class var reuseIdentifier: String { return NSStringFromClass(self).components(separatedBy: ".").last! }
}

